package com.zhang.storm.pv;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.Map;

public class PvBolt extends BaseRichBolt {

    private OutputCollector collector;


    @Override
    public void prepare(Map map, TopologyContext context, OutputCollector collector) {
        this.collector = collector;
    }

    private int pvnum = 0;
    @Override
    public void execute(Tuple input) {
        String str = input.getString(0);
        String[] splits = str.split("\t");
        String sessionId = splits[1];
        if(sessionId != null){
            pvnum ++;
            collector.emit(new Values(Thread.currentThread().getId(), pvnum));
        }
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("threadId", "pvnum"));
    }
}
